﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Security.Cryptography;
using System.Text;
using SQLExecute;
using System.IO;
using System.Net.Mail;
using System.Net.Mime;
using System.Text.RegularExpressions;

public partial class users_list_users : System.Web.UI.Page
{
    User u = new User();
    static int id;
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            if (Session["username"] != null && Session["fullname"] != null)
            {

                Init_Combo();
                BindGrid();
                lUsername.Text = Session["username"].ToString();
            }
            else Response.Redirect("../login/login.aspx");
        }
    }
    protected void Init_Combo()
    {

    /*
        cbGroup.Items.Insert(0, new ListItem("Users", "Users"));
        cbGroup.Items.Insert(1, new ListItem("Admin", "Admin"));
        cbGroup.SelectedIndex = 0;
    */

    }
    protected void BindGrid()
    {
        User u = new User();
        DataSet ds = u.Get_List_Users();
        gdUsers.DataSource = ds;
        gdUsers.DataBind();
        gdUsers.Columns[1].Visible = false;
    
    }
    protected void ClearTextControls()
    {
        txtFullname.Text = "";
        txtUserName.Text = "";
        txtEmail.Text = "";
    }   
    protected void btnAdd_Click(object sender, EventArgs e)
    {
        string s = string.Empty;
        string strPassword=string.Empty;
        if (txtFullname.Text != "")
        {
            if (u.ValidMail(txtEmail.Text))
            {

                strPassword = u.RandomPassword();
                s = u.Encrypt_Pass();
                u.FullName = txtFullname.Text;
                u.UserName = txtUserName.Text;
                u.Email = txtEmail.Text;

                if (chkActive.Checked)                                
                    u.Insert_New_User(s, true);
                else
                    u.Insert_New_User(s, false);                
                u.SendMail("quangtran@vus-etsc.edu.vn", txtEmail.Text, "Welcome to " + txtFullname.Text,1);
                ClearTextControls();
                BindGrid();

            }
            else
            {
                mpOKMsg.ShowMessage("Email không hợp lệ!");
            }
        }

       
    }
    private void Del_User(int nID)
    {
        string sql = "delete tbl_users where id='" + nID + "'";

        SqlHelper.ExecuteNonQuery(Common.strConnect, CommandType.Text, sql);



    }
    protected void gdUsers_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        Del_User(id);
    }
    protected void gdUsers_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        if (e.CommandName == "Delete")
        {
            id =Convert.ToInt16(e.CommandArgument.ToString());
            Del_User(id);
            BindGrid();
        }
    }
    protected void lnkLogout_Click(object sender, EventArgs e)
    {
        FormsAuthentication.SignOut();
       
        Session.Clear();
        Response.Redirect("../login/login.aspx");
    }
}
